using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using EventAbstractionAPI.RuleEngine;
using System.Threading;
//using Rhino.Mocks;
using System.Workflow.Runtime;

//The commandline for the xunit app, to test this assebly.
//C:\Program Files\xunit>xunit.console.exe "C:\Documents and Settings\rhwilbur\Desktop\Rule chaining\TestWorkFlowLibrary\WorkFlowTests\bin\Debug\WorkFlowTests.dll"
namespace WorkFlowTests
{
    public class Workflow
    {

        #region Constructor
        public Workflow()
        {
            InitializeWorkFlow();

            _type = typeof(EventAbstractionAPI.RuleEngine.BaseParserWorkflow);
            _items = new Dictionary<string, object>();

            ICapturedEvent event1 = new CapturedEvent();
            event1.EventName = "eventType2";

            ICapturedEvent event2 = new CapturedEvent();
            event2.EventName = "eventType3";

            ICapturedEvent event3 = new CapturedEvent();
            event3.EventName = "eventType2";

            ICapturedEvent event4 = new CapturedEvent();
            event4.EventName = "eventType3";

            ICapturedEvent event5 = new CapturedEvent();
            event5.EventName = "eventType2";

            ICapturedEvent event6 = new CapturedEvent();
            event6.EventName = "eventType2";

            _queue.Add(event1);
            _queue.Add(event2);
            _queue.Add(event3);
            _queue.Add(event4);
            _queue.Add(event5);
            _queue.Add(event6);

            _items.Add("Value3", _queue); //removed because _queue has nothing in it at this point.
    
        }

        private void InitializeWorkFlow()
        {
            _workflowRuntime = new WorkflowRuntime();
            _workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(WorkflowCompleted);
        } 

        private void StartWorkflow()
        {
            _workflowInstance = _workflowRuntime.CreateWorkflow(_type, _items);
            _workflowInstance.Start();
        }

        private void WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
        {
            _workflowCompleted = true;
            Console.WriteLine("Workflow completed");
        }

        #endregion

        [Fact(Skip = "true")]
        public void Test1()
        {
            StartWorkflow();

            int i = 0;
            while (!_workflowCompleted)
            {
                Thread.Sleep(1000); // pause for 1 second = 1000;

                if (i < 6)
                {
                    break;
                }
                else
                {
                    i++;
                }
            }

            Assert.True(_workflowCompleted,": The workflow was finished?");
        }

        Boolean _workflowCompleted = false;

        //MockRepository _mocks = new MockRepository();
        List<ICapturedEvent> _queue = new List<ICapturedEvent>();

        Type _type;
        Dictionary<string, object> _items;

        WorkflowRuntime _workflowRuntime = null;
        WorkflowInstance _workflowInstance = null;
    }
}
